import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:largefriends/pages/lf_edit_profile/lf_manage_photos/lf_manage_photos_logic.dart';
import 'package:largefriends/pages/lf_feedback/lf_feedback_logic.dart';
import 'package:largefriends/pages/lf_verify/lf_verify_logic.dart';
import 'package:largefriends/res/lf_color.dart';
import 'package:largefriends/res/lf_double_ext.dart';
import 'package:largefriends/res/strings/lf_strings.dart';
import 'package:largefriends/widgets/custom_cache_image.dart';
import 'package:shimmer/shimmer.dart';

import '../../res.dart';
import '../../res/lf_fonts.dart';
import '../../res/lf_text_style.dart';
import '../../service/lf_auth_service.dart';
import '../../utils/lf_scroll_behavior.dart';
import '../../widgets/dialog_action_tip.dart';
import '../../widgets/lf_app_bar.dart';
import '../../widgets/lf_button.dart';
import '../lf_base_controller.dart';
import 'lf_verify_camera/lf_verify_camera_logic.dart';
import 'lf_verify_id/lf_verify_id_logic.dart';

class LfVerifyPage extends StatefulWidget {
  const LfVerifyPage({super.key});

  @override
  State<LfVerifyPage> createState() => _LfVerifyPageState();
}

class _LfVerifyPageState extends State<LfVerifyPage> {
  final logic = Get.put(LfVerifyLogic());

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((call) {
      if (mounted && logic.isMust && (!logic.showedTips)) {
        logic.showedTips = true;
        Future.delayed(const Duration(seconds: 1), () {
          showActionTips(
              tips: strMustVerifyTip.tr,
              actionButtonTip: strOK.tr,
              action: () {},
              dismissible: false,
              showCancel: false);
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        child: Scaffold(
          appBar: lfAppBar(
              leadingCallback: logic.isMust
                  ? null
                  : () {
                      Get.back();
                    },
              centerTitle: false,
              title: strVerifyByVideoOrID.tr),
          body: GetBuilder<LfVerifyLogic>(
            builder: (logic) {
              return Padding(
                padding: EdgeInsets.symmetric(horizontal: 10.w),
                child: ScrollConfiguration(
                  behavior: LfScrollBehavior(),
                  child: CustomScrollView(
                    slivers: [
                      10.h.sliverBox,
                      _gestureView().sliverBox,
                      10.h.sliverBox,
                      Text(
                        strCopyGestureShortTip.tr,
                        style: lfTextStyle(
                          sp: 15.sp,
                          fontFamily: fontMedium,
                        ),
                        textAlign: TextAlign.center,
                      ).sliverBox,
                      30.h.sliverBox,
                      Padding(
                        padding: EdgeInsets.symmetric(horizontal: 20.w),
                        child: LfButton(
                          text: strVerifyNow.tr,
                          onPress: () {
                            checkPhoto(() {
                              toVerifyCamera(gesture: logic.gestureEntity!);
                            });
                          },
                          enabled: logic.gestureEntity != null,
                        ),
                      ).sliverBox,
                      10.h.sliverBox,
                      Center(
                        child: Text(
                          strOr.tr,
                          style: lfTextStyle(sp: 16.sp, fontFamily: fontBold),
                        ),
                      ).sliverBox,
                      10.h.sliverBox,
                      InkWell(
                        onTap: () {
                          checkPhoto(() {
                            toVerifyID();
                          });
                        },
                        child: Container(
                          margin: EdgeInsets.symmetric(horizontal: 40.w),
                          decoration: BoxDecoration(
                              borderRadius: 40.r.radius,
                              border:
                                  Border.all(color: colorFEFEFE, width: 2.r)),
                          padding: EdgeInsets.symmetric(vertical: 12.h),
                          alignment: Alignment.center,
                          child: Text(strVerifyByID.tr,
                              style: lfTextStyle(
                                  sp: 17.sp,
                                  fontFamily: fontBold,
                                  color: colorFEFEFE)),
                        ),
                      ).sliverBox,
                      40.h.sliverBox,
                      Text.rich(TextSpan(
                          text: strVerifyNote.tr.split("%%s").first,
                          style: lfTextStyle(
                            sp: 15.sp,
                            fontFamily: fontRegular,
                          ),
                          children: [
                            TextSpan(
                              text: strContactUs.tr,
                              style: lfTextStyle(
                                  sp: 15.sp,
                                  fontFamily: fontRegular,
                                  decoration: TextDecoration.underline),
                              recognizer: TapGestureRecognizer()
                                ..onTap = () {
                                  toFeedback(type: FeedbackType.account);
                                },
                            ),
                            TextSpan(
                              text: strVerifyNote.tr.split("%%s").last,
                            ),
                          ])).sliverBox,
                      20.h.sliverBox,
                      logic.isMust
                          ? Container(
                              alignment: Alignment.center,
                              padding: EdgeInsets.only(
                                bottom: 20.h,
                              ),
                              margin: EdgeInsets.only(bottom: 20.h),
                              child: InkWell(
                                  child: Text(
                                    strSignOut.tr,
                                    style: lfTextStyle(
                                        sp: 16.sp,
                                        fontFamily: fontBold,
                                        decoration: TextDecoration.underline,
                                        color: Colors.white),
                                  ),
                                  onTap: () {
                                    logic.authService.loginOut();
                                  }),
                            ).sliverBox
                          : 1.h.sliverBox,
                    ],
                  ),
                ),
              );
            },
          ),
        ),
        onWillPop: () async {
          if (logic.isMust) {
            return false;
          } else {
            Get.back();
            return false;
          }
        });
  }

  Widget _gestureView() {
    if (logic.loadingState == LoadingState.loading) {
      return Shimmer.fromColors(
        baseColor: color8F8F8F,
        period: const Duration(seconds: 1),
        highlightColor: color210215.withAlpha(10),
        child: Container(
          decoration:
              BoxDecoration(color: Colors.white, borderRadius: 10.r.radius),
          width: ScreenUtil().screenWidth - 20.w,
          height: (ScreenUtil().screenWidth - 20.w) * 480.0 / 686.0,

          ///服务器返回的图片尺寸比例
        ),
      );
    } else if (logic.loadingState == LoadingState.success &&
        logic.gestureEntity != null) {
      return CustomCacheImage(
        imageUrl: logic.gestureEntity!.pic,
        width: ScreenUtil().screenWidth - 20.w,
        height: (ScreenUtil().screenWidth - 20.w) * 480.0 / 686.0,

        ///服务器返回的图片尺寸比例
        radius: 10.r,
      );
    } else {
      return InkWell(
        child: Container(
          decoration:
              BoxDecoration(color: Colors.white, borderRadius: 10.r.radius),
          width: ScreenUtil().screenWidth - 20.w,
          padding: EdgeInsets.symmetric(horizontal: 10.w),
          height: (ScreenUtil().screenWidth - 20.w) * 480.0 / 686.0,

          ///服务器返回的图片尺寸比例
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Image.asset(
                Res.ic_failure,
                height: 70.w,
                fit: BoxFit.cover,
              ),
              10.h.sbh,
              Text(
                strSlowConnectionTip.tr,
                style: lfTextStyle(sp: 14.sp, fontFamily: fontRegular),
                textAlign: TextAlign.center,
              ),
              10.h.sbh,
              Text(
                strTapToTry.tr,
                style: lfTextStyle(sp: 14.sp, fontFamily: fontMedium),
                textAlign: TextAlign.center,
              )
            ],
          ),
        ),
        onTap: () {
          logic.getGestureCode();
        },
      );
    }
  }

  checkPhoto(Function next) {
    LfAuthService authService = Get.find();
    if (authService.currentAccount!.publicPhotos().isNotEmpty) {
      next.call();
    } else {
      showActionTips(
          actionButtonTip: strUploadPhoto.tr,
          tips: strCheckPhotoTip.tr,
          action: () {
            toManagePhotos(photoType: PhotoType.public);
          });
    }
  }
}
